package com.myapplication;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getSimpleName();
    private DiscoverScrollView mScrollView;
    private TestListView mListView;
    private CustomViewPager mPager;
    private TestFrameLayout mRootFrame;

    private View mHeaderView;
    private int mHeaderHeight;
    private View mPlaceHolderView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mScrollView = (DiscoverScrollView) findViewById(R.id.parent_scrollview);
        mScrollView.setInterceptTouchEvent(false);

        mRootFrame = (TestFrameLayout) findViewById(R.id.root_frame);

        List<String> list = new ArrayList<>();
        for (int i = 0; i < 20; ++i) {
            list.add(String.valueOf(i));
        }

        mListView = (TestListView) findViewById(R.id.list);

        mPlaceHolderView = LayoutInflater.from(this).inflate(R.layout.header, null);

        mListView.addHeaderView(mPlaceHolderView);

        mListView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, list));
        mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView absListView, int i) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                int scrollY = getScrollY(view);
                if (mHeaderView != null) {
                    mHeaderView.setTranslationY(Math.max(-scrollY, -mHeaderHeight));
                }
            }
        });

        mPager = (CustomViewPager) findViewById(R.id.pager);
        TestAdapter adapter = new TestAdapter(this);
        mPager.setAdapter(adapter);

        mRootFrame.setPager(mPager);

        mHeaderView = findViewById(R.id.header);
        mHeaderView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                if (bottom - top != oldBottom - oldTop) {
                    mHeaderHeight = mHeaderView.getHeight();
                    adjustHeaderPadding(mHeaderHeight);
                }
            }
        });
    }

    public void adjustHeaderPadding(int padding) {
        if (mPlaceHolderView != null) {
            mPlaceHolderView.setPadding(0, padding, 0, 0);
        }
    }

    public int getScrollY(AbsListView view) {
        View c = view.getChildAt(0);
        if (c == null) {
            return 0;
        }
        int firstVisiblePosition = view.getFirstVisiblePosition();
        int top = c.getTop();

        int headerHeight = 0;
        if (firstVisiblePosition >= 1) {
            headerHeight = mHeaderHeight;
        }

        return -top + firstVisiblePosition * c.getHeight() + headerHeight;
    }

    public class TestAdapter extends PagerAdapter {
        private String[] mItems = {"123", "456", "789"};
        private List<View> mList;
        private int[] colors = {R.color.white, R.color.black, R.color.grey};
        private float mLastX;
        private float mLastY;

        public TestAdapter(Context context) {
            super();
            mList = new ArrayList<>();
            for (int i = 0; i < mItems.length; ++i) {
                View view = LayoutInflater.from(context).inflate(R.layout.pager_item, null, false);
                view.setBackgroundColor(getResources().getColor(colors[i]));

                TextView tx = view.findViewById(R.id.item);

                tx.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View view, MotionEvent motionEvent) {
                        switch (motionEvent.getAction()) {
                            case MotionEvent.ACTION_DOWN:
                                mLastX = motionEvent.getX();
                                mLastY = motionEvent.getY();
                                Log.i(TAG, "mLastX x : " + mLastX + ", mLastY : " + mLastY);
                                break;

                            case MotionEvent.ACTION_UP:
                                float diffX = (motionEvent.getX() - mLastX);
                                float diffY = (motionEvent.getY() - mLastY);
                                Log.i(TAG, "diff x : " + diffX + ", diff y : " + diffY);
                                if (Float.compare(Math.abs(diffX), 0f) == 0 && Float.compare(Math.abs(diffY), 0f) == 0) {
                                    Toast.makeText(getApplicationContext(), "toast test", Toast.LENGTH_SHORT).show();
                                }
                                break;

                            default:
                                break;
                        }
                        return false;
                    }
                });
                tx.setText(mItems[i]);
                mList.add(view);
            }
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(mList.get(position));
            return mList.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mList.get(position));
        }

        @Override
        public int getCount() {
            return mItems.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    }
}
